package nc.bs.impl.yfk;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.util.HttpClientUtil;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.itf.yfk.IBIPAttachService;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.pubitf.para.SysInitQuery;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;
import nccloud.pubitf.platform.attachment.IAttachmentService;
import uap.pub.fs.domain.basic.FileHeader;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class BIPAttachServiceImpl implements IBIPAttachService {
    private BaseDAO baseDAO;
    private IFileSystemService fileSystemService;
    private IAttachmentService attachmentService;

    @Override
    public void uploadAttachToBIPURL(String pk_id, String fname, String userid, List<JSONObject> attURLs) throws BusinessException {
        if(attURLs == null || attURLs.isEmpty()) return;
        // 创建文件夹
        for(JSONObject json : attURLs){
            String fileURL = json.getString("attachmentPath");
            String type = json.getString("attachmentType");
            String name = json.getString("attachmentName");
            String attname =  name+"."+ type;
            // 特殊字符转义
            String attachmentName = attname.replaceAll("[<>:\"/\\\\|?*]", "_");
            NCFileVO ifileVO = new NCFileVO();
            ifileVO.setCreator(userid);
            ifileVO.setFileLen(0);
            ifileVO.setIsdoc(null);
            ifileVO.setFolder(false);
            ifileVO.setFiletype("url");
            ifileVO.setDirty(false);
            ifileVO.setPk_doc(fileURL);
            ifileVO.setTime(System.currentTimeMillis());
            ifileVO.setPath(pk_id+"/"+attachmentName); // 固定格式按照 "/"分割
            getFileSystemService().insertURLFileVO(ifileVO);
        }
        this.getBaseDAO().executeUpdate("insert into YFKATTACH_XLOG(PK_GLID) VALUES('"+pk_id+"')");
        this.getBaseDAO().executeUpdate("commit;");
    }

    @Override
    public void uploadAttachToBIPInputStream(String pk_id, String fname, String userid, List<JSONObject> attURLs) throws BusinessException {
        if(attURLs == null || attURLs.isEmpty()) return;
        String yfktbj = "http://"+ SysInitQuery.getParaString("GLOBLE00000000000000", "YFKTBJ");
        String url = yfktbj+ "/yfkService/downloadFileURLIO";
        String filepath = "";
        for(JSONObject json : attURLs){
            String type = json.getString("attachmentType");
            String name = json.getString("attachmentName").replace("/","_");
            String attname =  name+"."+ type;
            // 特殊字符转义
            String attachmentName = attname.replaceAll("[<>:\"/\\\\|?*]", "_");
            InputStream inputStream = null;
            long fileIOSize = 0;
            try {
                inputStream = HttpClientUtil.sendPostIS(url, json.toString());
                if(inputStream == null) continue;
                FileHeader header = this.getAttachmentService().upload(attachmentName, inputStream, false,0, "uapattachroot");
                filepath = pk_id+"/"+UUID.randomUUID(); // 由于一个报销单会存在名称重复的附件
                this.saveDBInfo(filepath, header, userid, fileIOSize);
                filepath = "";
            } catch (Exception e) {
                continue;
            }
                if(inputStream != null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        throw new BusinessException("文件流关闭异常:" + e.getMessage());
                    }
                }
        }
        this.getBaseDAO().executeUpdate("insert into YFKATTACH_XLOG(PK_GLID) VALUES('"+pk_id+"')");
        this.getBaseDAO().executeUpdate("commit;");
    }
    private NCFileNode saveDBInfo(String fullPath, FileHeader header, String userid,long fileIOSize) {
        IAttachmentService ncservice = NCLocator.getInstance().lookup(IAttachmentService.class);
        NCFileVO attach = new NCFileVO();
        attach.setPath(header.getName());
        attach.setCreator(userid);
        attach.setFileLen(header.getFileSize());
        attach.setPk_doc(header.getPath());
        attach.setIsdoc("z");
        NCFileNode node = ncservice.saveAttachDBInfo(fullPath, attach);
        return node;
    }
    public IFileSystemService getFileSystemService() {
        if(null == this.fileSystemService) {
            this.fileSystemService = NCLocator.getInstance().lookup(IFileSystemService.class);
        }
        return this.fileSystemService;
    }

    public IAttachmentService getAttachmentService() {
        if(null == this.attachmentService) {
            this.attachmentService = NCLocator.getInstance().lookup(IAttachmentService.class);
        }
        return this.attachmentService;
    }

    public BaseDAO getBaseDAO() {
        return this.baseDAO == null ? new BaseDAO() : this.baseDAO;
    }
}
